package Top200;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author zhangmin
 * @create 2022-02-25 9:36
 */
public class simplifyPath71 {
    public String simplifyPath(String path) {
        //将path用/分割--然后入栈
        String[] names = path.split("/");
        Deque<String> stack=new ArrayDeque<>();
        for (String name:names) {
            if (name.equals("..")){
                //遇到上级目录，出栈
                if (!stack.isEmpty()) {
                    stack.pollLast();
                }
            }else if(name.length()>0&&!".".equals(name)){
                stack.offerLast(name);
            }
        }
        StringBuffer sb=new StringBuffer();
        if (stack.isEmpty()){
            sb.append('/');
        }else {
            while (!stack.isEmpty()){
                sb.append('/').append(stack.pollFirst());
            }
        }
        return sb.toString();
    }
}
